﻿Imports System.Net.Sockets
Imports System.Text
Imports HslCommunication
Public Class Form1

    Private socketCore As Socket
    Private connectSuccess As Boolean = False
    Private buffer(2048) As Byte
    Private timer As System.Windows.Forms.Timer
    Private th As Threading.Thread
    Delegate Sub setReceivedMsgDele(msg As String)
    Dim setReceivedMsgCall As New setReceivedMsgDele(AddressOf setReceivedMsg)

    Public Sub New()
        ''加载DLL到exe的事件
        AddHandler AppDomain.CurrentDomain.AssemblyResolve, New ResolveEventHandler(AddressOf CurrentDomain_AssemblyResolve)
        ' 此调用是设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。

    End Sub

    Private Function CurrentDomain_AssemblyResolve(sender As Object, args As ResolveEventArgs) As System.Reflection.Assembly
        Dim dllName As String = If(args.Name.Contains(","), args.Name.Substring(0, args.Name.IndexOf(","c)), args.Name.Replace(".dll", ""))
        dllName = dllName.Replace(".", "_")
        If dllName.EndsWith("_resources") Then
            Return Nothing
        End If
        Dim rm As New System.Resources.ResourceManager([GetType].Namespace & ".Resources", System.Reflection.Assembly.GetExecutingAssembly())
        Dim bytes As Byte() = DirectCast(rm.GetObject(dllName), Byte())
        Return System.Reflection.Assembly.Load(bytes)
    End Function
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        socketCore?.Close()
        socketCore = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        connectSuccess = False
        th = New Threading.Thread(AddressOf socketStart)
        th.Start()
    End Sub
    Private Sub socketStart()

        If connectSuccess = True Then socketCore?.Close()
        socketCore.Connect(System.Net.IPAddress.Parse(HslCommunication.Core.HslHelper.GetIpAddressFromInput(TextBox1.Text)), Integer.Parse(TextBox2.Text))
        connectSuccess = True

        socketCore.BeginReceive(buffer, 0, 2048, SocketFlags.None, New AsyncCallback(AddressOf ReceiveCallBack), socketCore)
    End Sub

    Private Sub ReceiveCallBack(ar As IAsyncResult)
        Dim length As Integer = socketCore.EndReceive(ar)
        socketCore.BeginReceive(buffer, 0, 2048, SocketFlags.None, New AsyncCallback(AddressOf ReceiveCallBack), socketCore)
        If length = 0 Then Exit Sub
        Dim data(length) As Byte
        Array.Copy(buffer, 0, data, 0, length)
        Dim msg As String = Encoding.ASCII.GetString(data)
        Me.Invoke(setReceivedMsgCall, msg)
    End Sub
    Private Sub setReceivedMsg(msg As String)
        TextBox3.Text &= (String.Format("[收] {0}:{1}", Now.ToString, msg)) & vbCrLf
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim sendData As Byte()
        sendData = Encoding.ASCII.GetBytes(TextBox4.Text.Replace("\\n", "\r\n"))
        TextBox3.Text &= (String.Format("[发] {0}:{1}", Now.ToString, TextBox4.Text)) & vbCrLf

        socketCore?.Send(sendData, 0, sendData.Length, SocketFlags.None)
    End Sub
End Class